package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.FodderStorageDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.FodderStorage;
import cn.edu.scau.cmi.hombio.domain.FodderType;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for FodderStorage entities
 * 
 */

@Service("FodderStorageService")

@Transactional
public class FodderStorageServiceImpl implements FodderStorageService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages FodderStorage entities
	 * 
	 */
	@Autowired
	private FodderStorageDAO fodderStorageDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * Instantiates a new FodderStorageServiceImpl.
	 *
	 */
	public FodderStorageServiceImpl() {
	}

	/**
	 * Return all FodderStorage entity
	 * 
	 */
	@Transactional
	public List<FodderStorage> findAllFodderStorages(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<FodderStorage>(fodderStorageDAO.findAllFodderStorages(startResult, maxRows));
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public FodderStorage deleteFodderStorageEmployee(Integer fodderstorage_id, Integer related_employee_id) {
		FodderStorage fodderstorage = fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		fodderstorage.setEmployee(null);
		related_employee.getFodderStorages().remove(fodderstorage);
		fodderstorage = fodderStorageDAO.store(fodderstorage);
		fodderStorageDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return fodderstorage;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public FodderStorage saveFodderStorageEmployee(Integer id, Employee related_employee) {
		FodderStorage fodderstorage = fodderStorageDAO.findFodderStorageByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		fodderstorage.setEmployee(related_employee);
		related_employee.getFodderStorages().add(fodderstorage);
		fodderstorage = fodderStorageDAO.store(fodderstorage);
		fodderStorageDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return fodderstorage;
	}

	/**
	 * Save an existing FodderType entity
	 * 
	 */
	@Transactional
	public FodderStorage saveFodderStorageFodderType(Integer id, FodderType related_foddertype) {
		FodderStorage fodderstorage = fodderStorageDAO.findFodderStorageByPrimaryKey(id, -1, -1);
		FodderType existingfodderType = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderType != null) {
			existingfodderType.setId(related_foddertype.getId());
			existingfodderType.setName(related_foddertype.getName());
			existingfodderType.setSupply(related_foddertype.getSupply());
			existingfodderType.setMemo(related_foddertype.getMemo());
			related_foddertype = existingfodderType;
		}

		fodderstorage.setFodderType(related_foddertype);
		related_foddertype.getFodderStorages().add(fodderstorage);
		fodderstorage = fodderStorageDAO.store(fodderstorage);
		fodderStorageDAO.flush();

		related_foddertype = fodderTypeDAO.store(related_foddertype);
		fodderTypeDAO.flush();

		return fodderstorage;
	}

	/**
	 */
	@Transactional
	public FodderStorage findFodderStorageByPrimaryKey(Integer id) {
		return fodderStorageDAO.findFodderStorageByPrimaryKey(id);
	}

	/**
	 * Delete an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public void deleteFodderStorage(FodderStorage fodderstorage) {
		fodderStorageDAO.remove(fodderstorage);
		fodderStorageDAO.flush();
	}

	/**
	 * Delete an existing FodderType entity
	 * 
	 */
	@Transactional
	public FodderStorage deleteFodderStorageFodderType(Integer fodderstorage_id, Integer related_foddertype_id) {
		FodderStorage fodderstorage = fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage_id, -1, -1);
		FodderType related_foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype_id, -1, -1);

		fodderstorage.setFodderType(null);
		related_foddertype.getFodderStorages().remove(fodderstorage);
		fodderstorage = fodderStorageDAO.store(fodderstorage);
		fodderStorageDAO.flush();

		related_foddertype = fodderTypeDAO.store(related_foddertype);
		fodderTypeDAO.flush();

		fodderTypeDAO.remove(related_foddertype);
		fodderTypeDAO.flush();

		return fodderstorage;
	}

	/**
	 * Return a count of all FodderStorage entity
	 * 
	 */
	@Transactional
	public Integer countFodderStorages() {
		return ((Long) fodderStorageDAO.createQuerySingleResult("select count(o) from FodderStorage o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public Set<FodderStorage> loadFodderStorages() {
		return fodderStorageDAO.findAllFodderStorages();
	}

	/**
	 * Save an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public void saveFodderStorage(FodderStorage fodderstorage) {
		FodderStorage existingFodderStorage = fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage.getId());

		if (existingFodderStorage != null) {
			if (existingFodderStorage != fodderstorage) {
				existingFodderStorage.setId(fodderstorage.getId());
				existingFodderStorage.setQuantity(fodderstorage.getQuantity());
				existingFodderStorage.setTime(fodderstorage.getTime());
			}
			fodderstorage = fodderStorageDAO.store(existingFodderStorage);
		} else {
			fodderstorage = fodderStorageDAO.store(fodderstorage);
		}
		fodderStorageDAO.flush();
	}
}
